//
//  GBPopUpView.m
//  弹出框动画
//
//  Created by 郭斌 on 15/9/29.
//  Copyright (c) 2015年 郭斌. All rights reserved.
//

#import "GBPopUpView.h"
#import "GBMonitorTableview.h"

@interface GBPopUpView ()
{
    //显示的view
    UIView *mainView;
    GBMonitorTableview *tablev;
}

@end

@implementation GBPopUpView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        self.frame = CGRectMake(0, 0, screenW, screenH);
        //声明一个button，也可以用点击手势来代替
        UIButton *tapButton = [UIButton buttonWithType:0];
        tapButton.frame = CGRectMake(0, 0, screenW, screenH);
        tapButton.backgroundColor = [UIColor clearColor];
        [tapButton addTarget:self action:@selector(hideAnimation) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tapButton];
        
        /*
         * 在这里的mainview是具体显示内容的view
         * 需要在使用的时候，安装项目需求，单独定制
         * 在这里做的只是，优化弹出界面的动画
         */
        //创建小的显示的view
        mainView = [[UIView alloc] initWithFrame:frame];
        mainView.backgroundColor = [UIColor whiteColor];
        //创建阴影，显得好看一点
        mainView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor;
        mainView.layer.shadowOffset = CGSizeZero;
        mainView.layer.shadowOpacity = 1;
        mainView.layer.shadowRadius = 13;
        mainView.layer.borderWidth = 0.5;
        mainView.layer.borderColor = WQColor(110, 115, 120).CGColor;
        mainView.layer.cornerRadius = 5;
        
        [self addSubview:mainView];
        
        //创建tableview
        [self createTableview];
        
        //创建保存按钮
        [self createSaveBtn];
        
        //弹出时的动画
        [self showAnimaton];
    }
    return self;
}

#pragma mark - 创建tableview
-(void)createTableview
{
    tablev = [[GBMonitorTableview alloc] initWithFrame:CGRectMake(10, 10, mainView.frame.size.width - 20, mainView.frame.size.height - 50)];
    [mainView addSubview:tablev];
}

#pragma mark - 创建保存按钮
-(void)createSaveBtn
{
    UIButton *btn = [UIButton buttonWithType:0];
    btn.frame = CGRectMake(mainView.frame.size.width - 58, mainView.frame.size.height - 35, 50, 30);
    btn.layer.cornerRadius = 5;
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(AddTheNew) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.backgroundColor = KDColor;
    [mainView addSubview:btn];
}

#pragma mark - 添加新数据
-(void)AddTheNew
{
    [self.delegateShow showTheSelectResult];
    [self TapDisappear];
}

#pragma mark - 弹出时的动画
-(void)showAnimaton
{
    CGFloat d1 = 0.2, d2 = 0.15;
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
    //先放大后缩小动画
    [UIView animateWithDuration:d1 animations:^{
        
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:d2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            
            self.alpha = 1;
            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
            
        } completion:^(BOOL finished2) {
        }];
    }];
}

#pragma mark - 当前界面消失
-(void)TapDisappear
{
    [mainView removeFromSuperview];
    mainView = nil;
    [self removeFromSuperview];
}

#pragma mark - 当前界面消失
-(void)hideAnimation
{
    CGFloat d1 = 0.2, d2 = 0.15;
    //先放大后缩小动画
    [UIView animateWithDuration:d2 animations:^{
        
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
        
    } completion:^(BOOL finished){
        
        [UIView animateWithDuration:d1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            
            self.alpha = 0;
            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
            
        } completion:^(BOOL finished2){
            [self TapDisappear];
        }];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
